# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields, osv


class product_template(osv.osv):
    _name = "product.template"
    _description = "Product Template"
    _inherit = 'product.template'
    _order="name"
product_template()

class product_product(osv.osv):
    _name = "product.product"
    _inherit = 'product.product'
    _order="default_code,variants"
    
    def name_get(self, cr, user, ids, context={}):
        if not len(ids):
            return []
        code_only = False
        if context:
            if 'code_only' in context:
                code_only = context['code_only']
        def _name_get(d):
            name = d.get('name','')
            code = d.get('default_code',False)
            if code:
                if code_only:
                    name = code
                else:
                    name = '[%s] - %s' % (code,name)
            else:
                name = name
            return (d['id'], name)
        result = map(_name_get, self.read(cr, user, ids, ['name','default_code'], context))
        return result   
    
    def search(self, cr, user, args, offset=0, limit=None, order=None,  context=None, count=False):
        if context:
            #in context.keys() or 'prj_id' in context.keys():
            if context.has_key('project_id') or context.has_key('prj_id'):
                if 'project_id' in context.keys():
                    filter_value = context['project_id']
                else:
                    filter_value = context['prj_id']
                cr.execute("select \
                                pp.id\
                            from \
                                product_product pp\
                            left join\
                                kdvn_budget_data kbd on pp.kdvn_budget_id = kbd.budget_id\
                            where\
                                project_id=%d" % filter_value)
                res = cr.fetchall()
                res1=[]
                for tmp in res:
                    res1.append(tmp[0])
                var_filter=res1
                args=args+[('id','in',var_filter)]
        return super(product_product,self).search(cr, user, args, offset, limit, order,context, count)
    
    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=None):
        if not args:
            args=[]
        if not context:
            context={}

        if name:
            ids = self.search(cr, user, [('default_code','=',name)]+ args, limit=limit, context=context)
            if not len(ids):
                ids = self.search(cr, user, [('name','=',name)]+ args, limit=limit, context=context)
            if not len(ids):
                ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit, context=context)
            if not len(ids):
                ids = self.search(cr, user, [('default_code',operator,name)]+ args, limit=limit, context=context)                        
        else:
            ids = self.search(cr, user, args, limit=limit, context=context)
        result = self.name_get(cr, user, ids, context)
        return result

product_product()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
